package com.cn.suplayer.backTask;

import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.widget.ImageView;

import com.cn.suplayer.Util;

/**
 * Created by ZLM on 2016/6/17.
 * TODO: Write something
 */
public class BackTask extends AsyncTask<String, Void, Bitmap> {

    private ImageView tag;
    private int width;

    @Override
    protected Bitmap doInBackground(String... params) {
        String path = params[0];
        Bitmap bitmap = getVideoThumbnail(path, width, width, MediaStore.Video.Thumbnails.MINI_KIND);
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        showBitmap(bitmap);
    }

    public void showBitmap(Bitmap bitmap) {
        tag.setImageBitmap(bitmap);
    }

    public void setBitmapTag(ImageView tag) {
        this.tag = tag;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * 获取视频缩略图
     *
     * @param videoPath
     * @param width
     * @param height
     * @param kind
     * @return
     */
    private Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
        Bitmap bitmap = null;
        bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }
}
